package app;

import forms.MainForm;
import java.util.*;

/**
 * @author seberm
 */
public class Main {

	final public static String version = "1.0";
	final public static String author = "Sabart Otto";

	/**
	 * @param args Array witch command line arguments
	 */
	public static void main(String[] args) {
		for (int i = 0; i < args.length; i++) {
			if (args[i].equalsIgnoreCase("--version") || args[i].equalsIgnoreCase("-v")) {
				System.out.println(getInfo());
			}

			if (args[i].equalsIgnoreCase("--help") || args[i].equalsIgnoreCase("-h")) {
				System.out.println(getHelp());
			}

			if (args[i].equalsIgnoreCase("--rules") || args[i].equalsIgnoreCase("-r")) {
				System.out.println(getRules());
			}

			if (args[i].equalsIgnoreCase("--console") || args[i].equalsIgnoreCase("-c")) {
				startConsole();
			}
			/**
			 * @todo tady nemuze byt system exit... ukonci totiz cyklus..takze
			 * pokud se zada vice parametru... nic se nestane
			 */
			System.exit(0);
		}


		// Graphics interface
		new MainForm().setVisible(true);


	}

	public static void startConsole() {
		Scanner sc = new Scanner(System.in);

		System.out.println("Vitejte ve hre BlackJack (Oko bere; 21)\n\n");
		CPU pc = new CPU("CPU");

		System.out.println("Zadejte jmeno hrace:");
		User usr = new User(new String(sc.next()));

		do {
			System.out.println("\n\nHra zacala\n");

			pc.start();
//		   System.out.println("SMAZAT!!! - pocitac ma tento soucet karet: " + pc.cardsSum);
			usr.start();

			/**
			 * @todo dodelat kdo vyhral - objektove... aby to slo volat i z grafickeho formu!
			 */
			/**
			 * @todo Zjednodusit!
			 */
			if (pc.getCardsSum() <= 21 && usr.getCardsSum() > 21) {
				Main.win(pc, usr, false);
			} else if (pc.getCardsSum() > 21 && usr.getCardsSum() <= 21) {
				Main.win(usr, pc, false);
			} else if (usr.getCardsSum() > pc.getCardsSum()) {
				Main.win(usr, pc, false);
			} else if (pc.getCardsSum() > usr.getCardsSum()) {
				Main.win(pc, usr, false);
			} else {
				Main.win(pc, usr, true);
			}

			System.out.println("Prejete si dalsi hru? [a/n]");
		} while (sc.next().charAt(0) != 'n');

		System.exit(0);
	}

	/**
	 * Prints who wins the game
	 * @param win Who wins
	 * @param lose Who lost
	 * @param draw It's a draw?
	 */
	public static void win(Player win, Player lose, boolean draw) {
		if (!draw) {
			System.out.println("\n\nVyhral " + win.getName() + " se skore: " + win.getCardsSum());
			System.out.println("Skore prohraneho: " + lose.getCardsSum());
			win.winners++;
		} else {
			System.out.println("Remiza hracu. Skore lose: " + lose.getCardsSum() + " a skore win: " + win.getCardsSum());
		}
	}

	/**
	 * Prints the help of application
	 *
	 * @return
	 */
	public static String getHelp() {
		return new String("Napoveda:...k prikazum a spousteni\n");
	}

	/**
	 * Prints the rules of the game
	 *
	 * @return
	 */
	public static String getRules() {
		return new String("tady jsou nejaka pravidla k blackjack");
	}

	/**
	 * Prints info about application
	 *
	 * @return
	 */
	public static String getInfo() {
		return new String("Author: " + Main.author + "\nVersion: " + Main.version);
	}
}
